package cn.myframe.controller.sys;

import cn.myframe.controller.AbstractController;
import cn.myframe.controller.BaseController;
import cn.myframe.entity.sys.SysMenuEntity;
import cn.myframe.service.BaseService;
import cn.myframe.service.sys.SysMenuService;
import cn.myframe.utils.Constant;
import cn.myframe.utils.annotation.IgnoreAuth;
import cn.myframe.utils.core.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 菜单管理
 * 
 * @author ynz
 * @email ynz@myframe.cn
 * @date 2018-08-01 17:05:53
 */
@RestController
@RequestMapping("/sys/menu")
@IgnoreAuth
public class SysMenuController  extends AbstractController implements BaseController<SysMenuEntity,BaseService<SysMenuEntity>>{
	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * 所有菜单列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public List<SysMenuEntity> list(){
		List<SysMenuEntity> menuList = sysMenuService.queryList(new HashMap<>());

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public R select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 角色授权菜单
	 */
	@RequestMapping("/perms")
	@RequiresPermissions("sys:menu:perms")
	public R perms(){
		//查询列表数据
		List<SysMenuEntity> menuList = null;
		
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() == Constant.SUPER_ADMIN){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("removeId", "412");
			map.put("infoId", "428");
			map.put("genderId", "28");
			menuList = sysMenuService.queryList(map);
		}else{
			menuList = sysMenuService.queryUserList(getUserId());
		}
		
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public R info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.queryObject(menuId);
		return R.ok().put("menu", menu);
	}
	

	
	/**
	 * 用户菜单列表
	 */
	@RequestMapping("/user")
	//@IgnoreAuth
	public R user(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		Set<String> permissions = sysMenuService.getUserPermissions(getUserId());
		
		//验证授权文件
		Set<String> set = new HashSet<>();
		//放开授权
		set.addAll(permissions);
		//进行授权处理
	/*	if(PjAuthorizeUtil.isOverdue()){
		    for(String perm :permissions){
		       if(perm.indexOf("list") >= 0 && perm.indexOf("sysblacklist") == -1
		               && perm.indexOf("busprogramlist") == -1){
		           set.add(perm);
		       }
		    }		    
		}else{
		    set.addAll(permissions);
		}*/
		
		return R.ok().put("menuList", menuList).put("permissions", set);
	}
	
	

	@Override
	public BaseService<SysMenuEntity> getService() {
		// TODO Auto-generated method stub
		return sysMenuService;
	}
	
   
	
}
